package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class LoginDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String userName = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();
        try (Connection connection = DButil.getConnection()) {
            Statement statement = connection.createStatement();
            String sqlUName = "SELECT username FROM user WHERE username = '" + userName + "'";
            ResultSet rsUName = statement.executeQuery(sqlUName);
            if (rsUName.next()) {
                if (rsUName.getString("username").equals(userName)) {
                    String sqlPwd = "SELECT password  FROM user  WHERE password = '" + password + "'";
                    ResultSet rsPwd = statement.executeQuery(sqlPwd);
                    if (rsPwd.next()) {
                        if (rsPwd.getString("password").equals(password)) {
                            String sql = "SELECT id,username,password,nickname,age " +
                                    "FROM user " +
                                    "WHERE username = '" + userName +
                                    "' AND password = '" + password + "'";
                            ResultSet rs = statement.executeQuery(sql);
                            if (rs.next()) {
                                System.out.println("登录成功！！！");
                                int id = rs.getInt("id");
                                String uName = rs.getString("username");
                                String psd = rs.getString("password");
                                String nickName = rs.getString("nickname");
                                int age = rs.getInt("age");
                                String line = String.join(",", "id:" + id, "用户名:" + uName, "密码:" + psd, "昵称:" + nickName, "年龄:" + age);
                                System.out.println(line);
                            } else {
                                System.out.println("密码错误！！！");
                            }
                        } else {
                            System.out.println("密码错误！！！");
                        }
                    } else {
                        System.out.println("密码错误！！！");
                    }
                } else {
                    System.out.println("用户名错误！！！");
                }
            }


//            String sql = "SELECT id,username,password,nickname,age " +
//                    "FROM user " +
//                    "WHERE username = '"+ userName +
//                    "' AND password = '"+ password +"'";
//            ResultSet rs = statement.executeQuery(sql);
//            if (rs.next() == false){
//                System.out.println("用户名或密码不正确！！！");
//            }else {
//                int id = rs.getInt("id");
//                String uName = rs.getString("username");
//                String psd  = rs.getString("password");
//                String nickName = rs.getString("nickname");
//                int age = rs.getInt("age");
//                String line = String.join(",","id:"+id,"用户名:"+uName,"密码:"+psd,"昵称:"+nickName,"年龄:"+age);
//                System.out.println(line);
//            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
